/*
 * Created by JFormDesigner on Tue Jul 02 10:42:38 CST 2024
 */

package com.vict.gui;

import com.vict.GuiApplication;
import com.vict.exce.MessageEx;
import com.vict.service.DbService;
import lombok.extern.slf4j.Slf4j;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author Brainrain
 */
@Slf4j
public class AddDbConfigGui extends JFrame {
    public AddDbConfigGui() {
        initComponents();
    }

    private void saveAction(ActionEvent e) {
        String name = this.textFieldName.getText();
        String url = this.textFieldUrl.getText();
        String username = this.textFieldUsername.getText();
        String password = this.textFieldPassword.getText();
        try{
            DbService.dbService.addDbConfig(name, url, username, password);

            GuiApplication.guiApplication.getDbEditGui().update();
            GuiApplication.guiApplication.getDbEditGui().setVisible(true);
            this.setVisible(false);
        }catch(MessageEx ex){
            JOptionPane.showMessageDialog(this, ex.getMsg());
            log.error("新增异常", ex);
        }catch(Exception ex2){
            log.error("新增异常", ex2);
        }
    }

    private void thisWindowClosing(WindowEvent e) {
        GuiApplication.guiApplication.getDbEditGui().setVisible(true);
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        textFieldUrl = new JTextField();
        textFieldUsername = new JTextField();
        textFieldPassword = new JTextField();
        button1 = new JButton();
        label4 = new JLabel();
        textFieldName = new JTextField();

        //======== this ========
        setTitle("\u65b0\u589e\u6570\u636e\u5e93\u914d\u7f6e");
        setResizable(false);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                thisWindowClosing(e);
            }
        });
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("url:");
        label1.setHorizontalAlignment(SwingConstants.RIGHT);
        contentPane.add(label1);
        label1.setBounds(20, 75, 70, label1.getPreferredSize().height);

        //---- label2 ----
        label2.setText("username:");
        label2.setHorizontalAlignment(SwingConstants.RIGHT);
        contentPane.add(label2);
        label2.setBounds(20, 110, 70, label2.getPreferredSize().height);

        //---- label3 ----
        label3.setText("password:");
        label3.setHorizontalAlignment(SwingConstants.RIGHT);
        contentPane.add(label3);
        label3.setBounds(20, 145, 70, 17);
        contentPane.add(textFieldUrl);
        textFieldUrl.setBounds(110, 70, 165, 25);
        contentPane.add(textFieldUsername);
        textFieldUsername.setBounds(110, 105, 165, 25);
        contentPane.add(textFieldPassword);
        textFieldPassword.setBounds(110, 140, 165, 25);

        //---- button1 ----
        button1.setText("\u4fdd\u5b58");
        button1.addActionListener(e -> saveAction(e));
        contentPane.add(button1);
        button1.setBounds(new Rectangle(new Point(195, 200), button1.getPreferredSize()));

        //---- label4 ----
        label4.setText("name:");
        label4.setHorizontalAlignment(SwingConstants.RIGHT);
        contentPane.add(label4);
        label4.setBounds(20, 40, 70, 17);
        contentPane.add(textFieldName);
        textFieldName.setBounds(110, 35, 165, 25);

        contentPane.setPreferredSize(new Dimension(300, 255));
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JTextField textFieldUrl;
    private JTextField textFieldUsername;
    private JTextField textFieldPassword;
    private JButton button1;
    private JLabel label4;
    private JTextField textFieldName;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
